package pers.brozen.anka.service;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import pers.brozen.anka.dao.entity.AnkaSession;
import pers.brozen.anka.dao.entity.WechatUser;
import pers.brozen.anka.dao.repository.AnkaSessionRepository;
import pers.brozen.anka.utils.UUIDUtils;

import java.time.Duration;
import java.time.Instant;

/**
 * @author Brozen
 * @since 2024-05-28
 */
@Service
public class SessionService {

    @Resource
    private AnkaSessionRepository sessionRepo;

    @Value("")
    private Duration sessionTtl = Duration.ofHours(2);


    public AnkaSession createWechatMiniProgramSession(WechatUser user) {
        String sessionId = UUIDUtils.randomID();
        AnkaSession session = new AnkaSession();
        session.setUserId(user.getId().toString());
        session.setSessionId(sessionId);
        session.setExpiryAt(Instant.now().plusSeconds(sessionTtl.toSeconds()));
        sessionRepo.save(session);

        return session;
    }


}
